<?php
namespace app\index\controller;
use think\Controller;
class Vip extends Controller{
	//个人中心
	public function myself(){
		$where = [
			'nickname'=>session('nickname')
		];
		$data = model('Vip')->alias('v')
						->join('Village a','v.area=a.id')
						->where($where)->find()->toArray();
		// dump($data);
		if($data['icd'] != ''){
			$data['age'] = substr($data['icd'],6,4);
			$data['birth'] = substr($data['icd'],10,4);
		}
		$data['renter'] = model('Vip')->alias('v')
						->join('Contract c','c.vid=v.vid')
						->where($where)->count();
		$this->assign('data',$data);
		return $this->fetch();
	}

	//编辑个人资料
	public function edit(){
		foreach(model('Village')->where(['pid'=>'1'])->select() as $val){
			$data_area[] = $val->toArray();
		}
		// dump($data_area);
		$this->assign('data_area',$data_area);

		$where = [
			'nickname'=>session('nickname')
		];
		$data = model('Vip')->alias('v')
						->join('Village a','v.area=a.id')
						->where($where)->find()->toArray();
		// dump($data);
		$this->assign('data',$data);
		return $this->fetch();
	}

	//处理实名认证
	public function do_realname1(){
		// $this->success($_POST);exit;
		$res = model('Vip')->where(['nickname'=>$_POST['nickname']])->update($_POST);
		if($res > 0){
			$this->success('成功');
		}else{
			$this->error($res);
		}
		
	}

	//处理编辑
	public function do_edit(){
		$vld = $this->validate(input('post.'),'Vip.edit');
		if($vld !== true){
			$this->error($vld);
		}
		$res = model('Vip')->allowfield(true)->where(['nickname'=>session('nickname')])->update(input('post.'));
		if($res > 0){
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}
	
	//租房信息
	public function rent_info(){
		$where = [
			'nickname'=>session('nickname')
		];
		$data = model('Vip')->alias('v')
						->join('Village a','v.area=a.id')
						->join('Contract c','c.vid=v.vid')
						->join('Renter r','r.rid=c.rid')
						// ->join('Renter t','t.vill=a.id')
						->where($where)->select();
		if($data != []){
			foreach($data as $k=>$val){
				$data_rs[] = $val->toArray();
			}
			// dump($data_rs);exit;
			foreach($data_rs as $va){
				$va['vill'] = model('Village')->where(['id'=>$va['vill']])->find()->name;
				$res[] = $va;
			}
			// dump($res);exit;
			$this->assign('data_rs',$res);
		}
		
		return $this->fetch();
	}

	//历史经纪人
	public function p_member(){
		//租房经纪人
		$info1s = model('Vip')->alias('v')
						->join('Contract c','c.vid=v.vid')
						->where(['nickname'=>session('nickname')])->select();
		if(!empty($info1s)){
			foreach($info1s as $val){
				$info1[] = $val->uid;
			}
			$info1 = array_values(array_unique($info1));
			foreach($info1 as $uid){
				$data1[] = model('Member')->alias('m')->join('Village v','v.id=m.area')->join('Group g','g.gid=m.gid')->where(['uid'=>$uid])->find()->toArray();
			}
			$this->assign('data1',$data1);
		}
		

		//服务人员
		$info2s = model('Vip')->alias('v')
						->join('Service s','s.vid=v.vid and sid>1 and s.statu=2')
						->where(['nickname'=>session('nickname')])->select();
		if(!empty($info2s)){
			foreach($info2s as $val){
				$info2[] = $val->mid;
			}
			$info2 = array_values(array_unique($info2));		
			foreach($info2 as $uid){
				$data2[] = model('Member')->alias('m')->join('Village v','v.id=m.area')->join('Group g','g.gid=m.gid')->where(['uid'=>$uid])->find()->toArray();
			}
		}else{
			$data2 = [];
		}
		
		$this->assign('data2',$data2);

		return $this->fetch();
	}


	//推荐经纪人
	public function c_member(){
		$where = [
			'nickname'=>session('nickname')
		];
		$datas = model('Vip')->alias('v')
						->join('Village a','v.area=a.id')
						->join('Member m','m.area=v.area')
						->join('Group g','g.gid=m.gid')
						->where($where)->select();
		foreach($datas as $val){
			$data[] = $val->toArray();
		}
		$this->assign('datas',$data);
		return $this->fetch();
	}

	//我的点评
	public function mycomment(){
		$where = [
			'nickname'=>session('nickname')
		];
		$res = model('Service')->alias('s')
							->join('Vip v','v.vid=s.vid')
							->join('ServiceIni i','i.sid=s.sid')
							->join('Renter r','r.rid=s.rid')
							->where($where)->order('requesttime desc')
							->select();
		if($res != []){
			foreach($res as $val){
				if($val['mid']>0){
					$uname = model('Member')->where(['uid'=>$val['mid']])->find()->uname;
					$val['uname'] = $uname;
				}
				$data[] = $val->toArray();
			}
			// dump($data);exit;
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	//处理服务订单
	public function want(){
		if($_GET['want'] == 'cancel'){
			$res = model('Service')->where(['id'=>$_GET['id']])->update(['statu'=>3]);
			if($res > 0){
				$this->success('订单取消');
			}
		}elseif($_GET['want'] == 'comment'){
			$this->success($_GET['id']);
		}
	}

	//评价
	public function do_comment(){
		$comtime = date('Y-m-d H:i:s',time());
		$res = model('Service')->where(['id'=>$_POST['id']])->update(['comment'=>$_POST['comment'],'statu'=>2,'comtime'=>$comtime]);
		if($res>0){
			$this->success('评论成功');
		}else{
			$this->success($res);
		}
	}

	//check1
	public function check1(){
		if(input('get.nickname') == session('nickname')){
			echo 'true';
		}else{
			$res = model('Vip')->where(['nickname'=>input('get.nickname')])->count();
			if($res > 0){
				echo 'false';
			}else{
				echo 'true';
			}
		}
	}

	//check2
	public function check2(){
		$name = model('Vip')->where(['tel'=>input('get.tel')])->find();
		$name = $name['nickname'];
		if($name == session('nickname')){
			echo 'true';
		}else{
			$res = model('Vip')->where(['tel'=>input('get.tel')])->count();
			if($res > 0){
				echo 'false';
			}else{
				echo 'true';
			}
		}
		
	}

}
